var _controller;

function listButtonInit(controller) {
    this._controller = controller;

    //给Add按钮绑定事件
    $('#demo-add-row').click(function () {
        jumpTo("/" + controller + "/index");
    });

    //给Edit按钮绑定事件
    $('#demo-edit-row').click(function () {
        var _length = 0, _id;

        $("input[type='checkbox']:checked").each(function () {
            _length++;
            _id = $(this).val();
        });

        if (_length !== 1) {
            showInfoAlert("请选择一条记录进行编辑！", null);
            return false;
        }

        jumpTo("/" + controller + "/index?id=" + _id);
    });

    //给Delete按钮绑定事件
    $('#demo-delete-row').click(function () {
        showInfoAlertSelect("你确定要删除所选记录吗?", deleteFunc);
    });
}

// 删除（批量）
function deleteFunc() {
    var _selectids = "";

    $("input[type='checkbox']:checked").each(function () {
        _selectids += $(this).val() + ",";
    });

    if (_selectids === "") {
        showInfoAlert("请至少选择一条记录进行删除！", null);
        return false;
    }

    $.ajax({
        url: "/" + this._controller + "/deleteInfo",
        type: "POST",
        data: {ids: _selectids},
        async: false,
        error: function (request) {
            //提交失败
            showInfoAlert("服务器连接失败，请检查网络环境！", null);
        },
        success: function (data) {
            var r = eval("(" + data + ")");
            if (r.returnCode === "1") {
                showInfoAlert(decodeURI(r.returnMessage), jumpToIndex);
            } else {
                showInfoAlert(decodeURI(r.returnMessage), null);
            }
        }
    });
}

// 编辑
function editFunc(id) {
    window.location = "/" + this._controller + "/index?id=" + id;
}

// 查看详情
function showInfoFunc(id, tableName) {
    $.ajax({
        url: "/" + this._controller + "/show",
        type: "POST",
        data: {id: id, tableName: tableName},
        async: false,
        error: function (request) {
            //error
        },
        success: function (data) {
            $('setdata-content').html(data);
        }
    });
}

//跳转
function jumpToIndex() {
    window.location = "/" + this._controller + "/index_role";
}


// 删除单条记录
// 使用此方法前提条件：必须有handle_id、handle_url两个隐藏字段
// 二次确认
function gotoDeleteById(id) {
    $("input[name='handle_id']").val(id);

    showInfoAlertSelect("你确定要删除当前记录吗？", deleteFuncById);
}

function deleteFuncById() {
    $.ajax({
        url: "/" + this._controller + "/deleteById",
        type: "POST",
        data: {id: $("input[name='handle_id']").val()},
        async: false,
        error: function (request) {
            showInfoAlert("服务器连接失败，请检查网络环境！", null);
        },
        success: function (data) {
            var r = eval("(" + data + ")");

            if (r.returnCode === "1") {
                window.location = $("input[name='handle_url']").val();
            } else {
                showInfoAlert(decodeURI(r.returnMessage), null);
            }

            $("input[name='handle_id']").val("");
        }
    });
}

// 启停用某条记录
// 使用此方法前提条件：handle_url 隐藏字段
function gotoChangeFlag(id, status) {
    $.ajax({
        url: "/" + this._controller + "/changeFlag",
        type: "POST",
        data: {id: id, status: status},
        async: false,
        error: function (request) {
            showInfoAlert("服务器连接失败，请检查网络环境！", null);
        },
        success: function (data) {
            var r = eval("(" + data + ")");

            if (r.returnCode === "1") {
                window.location = $("input[name='handle_url']").val();
            } else {
                showInfoAlert(decodeURI(r.returnMessage), null);
            }
        }
    });
}

// 改变记录状态
// 使用此方法前提条件：必须有handle_id、handle_url两个隐藏字段
// 二次确认
function gotoChangeStatus(id, status, word) {
    $("input[name='handle_id']").val(id);
    $("input[name='handle_status']").val(status);

    showInfoAlertSelect("你确定要" + word + "吗？", changeStatusFunc);
}

function changeStatusFunc() {
    $.ajax({
        url: "/" + this._controller + "/changeStatus",
        type: "POST",
        data: {
            id: $("input[name='handle_id']").val(),
            status: $("input[name='handle_status']").val()
        },
        async: false,
        error: function (request) {
            showInfoAlert("服务器连接失败，请检查网络环境！", null);
        },
        success: function (data) {
            var r = eval("(" + data + ")");

            if (r.returnCode === "1") {
                window.location = $("input[name='handle_url']").val();
            } else {
                showInfoAlert(decodeURI(r.returnMessage), null);
            }

            $("input[name='handle_id']").val("");
            $("input[name='handle_status']").val("");
        }
    });
}